package com.liu.file.table;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/**
 * 导出一个表格为图片
 */
public class OutputTable {
	private Part part;

	public OutputTable(Part part) {
		this.part = part;
	}

	public PdfPTable generateTable() throws DocumentException, IOException {
		String[] strs = part.getPosition().split(";");
		float tableX = Float.parseFloat(strs[0]);
		// 获取列的数量
		int thNum = part.getThs().size();
		PdfPTable table = new PdfPTable(thNum);
		// 设置列的宽度
		float[] COLUMNS = new float[part.getThs().size()];
		for (int n = 0; n < thNum; n++) {
			COLUMNS[n] = part.getThs().get(n).getWidth();
		}

		table.setWidths(COLUMNS);

		float x = tableX;
		// 填入表头
		for (int n = 0; n < thNum; n++) {
			if (n > 0)
				x += COLUMNS[n - 1];
			table.addCell(getCell(part.getThs().get(n).getDivs(), true, x, COLUMNS[n]));
		}

		if (part.getTrs().get(0).getTds().size() == 1) {
			table.addCell(getCell(part.getTrs().get(0).getTds().get(0).getDivs(), false, x, COLUMNS[0]));
		} else {

			// 填入表格的内容
			for (int n = 0; n < part.getTrs().size(); n++) {
				x = tableX;
				for (int m = 0; m < thNum; m++) {
					if (m > 0)
						x += COLUMNS[m - 1];
					table.addCell(getCell(part.getTrs().get(n).getTds().get(m).getDivs(), false, x, COLUMNS[m]));
				}
			}
		}

		/*
		 * if(!"".equals(part.getTrs().get(0).getTds().get(0).getDivs().get(0).
		 * getData())){
		 * 
		 * }
		 */
		table.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.setWidthPercentage(100);
		return table;
	}

	private PdfPCell getCell(List<DIV> divs, boolean isTH, float x, float width)
			throws BadElementException, MalformedURLException, IOException {
		// 如果单元格中只有一个元素，则直接插入元素，否则创建一个table装入所有的div元素
		PdfPCell cell = null;
		if (divs.size() == 1) {
			cell = getSimpleCell(divs.get(0), isTH, x);
		} else {
			PdfPTable table = new PdfPTable(divs.size());

			float[] COLUMNS = new float[divs.size()];
			float beforeWidth = 0;
			for (int n = 0; n < divs.size(); n++) {
				COLUMNS[n] = Float.parseFloat(divs.get(n).getStyle().get("left"));
			}
			for (int n = 0; n < divs.size() - 1; n++) {
				beforeWidth += (COLUMNS[n] = COLUMNS[n + 1] - COLUMNS[n]);
			}
			COLUMNS[divs.size() - 1] = width - beforeWidth;
			try {
				table.setWidths(COLUMNS);
			} catch (DocumentException e) {
				e.printStackTrace();
			}
			for (int n = 0; n < divs.size(); n++) {
				cell = getSimpleCell(divs.get(n), isTH, COLUMNS[n]);
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				table.addCell(cell);
			}

			cell = new PdfPCell(table);
			cell.setBorder(0);
			if (!isTH)
				cell.setPadding(1f);
		}
		return cell;
	}

	private PdfPCell getSimpleCell(DIV div, boolean isTH, float x)
			throws BadElementException, MalformedURLException, IOException {
		PdfPCell cell = new PdfPCell();
		if ("img".equals(div.getType())) {
			Image img = Image.getInstance(Style.getImgPath() + div.getData());
			// img.scalePercent(0.5f);
			img.scalePercent(90f);
			Chunk chunk = new Chunk(img, 0, 0);
			cell.setPhrase(new Phrase(chunk));
		} else {
			cell.setPhrase(new Phrase(div.getData(), isTH ? Style.getDefaultTHFont() : Style.getDefaultTDFont()));
		}
		Style.addStyle(cell, div.getStyle(), isTH, x, div);
		return cell;
	}
}
